/*
 * ChessPiece.cpp
 *
 *  Created on: Mar 6, 2011
 *      Author: Michael Ziminsky
 */

#include "ChessPiece.h"
#include <cstring>
#include <cxxabi.h>

namespace model
{
	namespace piece
	{
		ChessPiece::ChessPiece(bool light) :
			isLight(light)
		{
		}

		ChessPiece::ChessPiece(const ChessPiece& orig) :
			isLight(orig.isLight)
		{
		}

		void ChessPiece::setLocation(const Location* const l)
		{
			currLoc = l;
		}

                const Location* ChessPiece::getLocation() const
		{
			return currLoc;
		}

		bool ChessPiece::cellCheck(const ChessBoard& b, const Location& loc, myTypes::moveSet& moves) const
		{
			bool cont = b.hasLocation(loc);
			if (cont)
			{
				myTypes::cPiece temp = b.getPieceAt(loc);
				if (!temp || !(cont = temp->isLight == isLight)) moves->insert(loc);
				else cont = false;
			}
			return cont;
		}

		std::ostream& operator <<(std::ostream& out, const ChessPiece& p)
		{
			char* name = __cxxabiv1::__cxa_demangle(typeid(p).name(), NULL, 0, 0);
			if (name) out << strrchr(name, ':') + 1;
			else out << "Chesspiece";
			free(name);
			return out;
		}

		Location getOffsetLocation(Direction d, const Location& orig, int xDist, int yDist)
		{
			if (!yDist) yDist = xDist;
			char newX = orig.col;
			int newY = orig.row;
			switch (d)
			{
				case LEFT:
					newX -= xDist;
					break;
				case UP:
					newY += yDist;
					break;
				case RIGHT:
					newX += xDist;
					break;
				case DOWN:
					newY -= yDist;
				case LEFTUP:
					newX -= xDist;
					newY += yDist;
					break;
				case RIGHTUP:
					newX += xDist;
					newY += yDist;
					break;
				case RIGHTDOWN:
					newX += xDist;
					newY -= yDist;
					break;
				case LEFTDOWN:
					newX -= xDist;
					newY -= yDist;
					break;
			}
			return Location(newX, newY);
		}
	} // namespace pieces
} // namespace model
